package com.framework.loippi.utils.kuaidi100;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.framework.loippi.utils.JsonUtils;
import com.framework.loippi.utils.MD5Utils;
import com.framework.loippi.utils.StringUtils;
import com.framework.loippi.utils.kuaidi100.utils.MD5;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class KuaiDiUtils {
    /**分配给贵司的的公司编号 */
    private final static String CUSTOMER = "6424193584CB30BF4BC6365E07C79020";
    /** key*/
    private final static String KEY = "LAjGOhSp2915";

    private final static String URL = "https://poll.kuaidi100.com/poll/query.do";


    /**
     *
     * @param com 查询的快递公司的编码， 一律用小写字母
     * @param num 查询的快递单号， 单号的最大长度是32个字符
     * @return
     */
    public static Map<String, Object> query(String com, String num) {
        Map<String, Object> map = new HashMap<>();
        try {
            Map<String, String> param = new HashMap<>();
            param.put("com", com);
            param.put("num", num);
            param.put("resultv2", "1");
            String paramString = JsonUtils.toJson(param);
            param = new HashMap<>();
            param.put("sign", MD5.encode(paramString + KEY + CUSTOMER));
            param.put("param", paramString);
            param.put("customer", CUSTOMER);
            String post = executePost(URL, param);
            ObjectMapper mapper = new ObjectMapper();
            map = mapper.readValue(post, Map.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return map;
    }






    /**
     * 发送 post请求访问本地应用并根据传递参数不同返回不同结果
     */
    public static String executePost(String actionUrl, Map<String, String> params) {

        CloseableHttpClient httpclient = HttpClients.createDefault();

        String result = null;

        HttpPost httppost = new HttpPost(actionUrl);

        List<NameValuePair> formparams = new ArrayList<NameValuePair>();

        for (String key : params.keySet()) {
            formparams.add(new BasicNameValuePair(key, params.get(key)));
        }

        UrlEncodedFormEntity uefEntity;
        try {
            uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
            httppost.setEntity(uefEntity);
            CloseableHttpResponse response = httpclient.execute(httppost);
            try {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    result = EntityUtils.toString(entity, "UTF-8");
                    return result;
                }
            } finally {
                response.close();
            }
        } catch (IOException e) {
            return null;
        } finally {
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }

        return result;
    }

    public static void main(String[] args) {
        Map<String, Object> zhongtong = query("zhongtong", "75162442114856");
        System.out.println(zhongtong);
    }

}
